package com.jf.apply_car.controller;

import com.jf.apply_car.bean.Foreign;
import com.jf.apply_car.service.ForeignService;
import com.jf.apply_car.util.MyConstants;
import com.jf.apply_car.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ForeignService foreignService;

    @GetMapping("/list")
    public String list() {
        return "category/category-list";
    }

    @GetMapping("/edit")
    public String edit() {
        return "category/category-edit";
    }

    @GetMapping("/page")
    @ResponseBody
    public ResultJson page(int page, int limit, Foreign foreign) {
        foreign.setType(MyConstants.CATEGORY_TYPE);
        return foreignService.getPage(page, limit, foreign);
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResultJson delete(int[] ids) {
        foreignService.delete(ids, MyConstants.CATEGORY_TYPE);
        return ResultJson.success();
    }

    @GetMapping("/info/{id}")
    @ResponseBody
    public ResultJson info(@PathVariable int id) {
        return foreignService.getById(id);
    }

    @PostMapping("/update")
    @ResponseBody
    public ResultJson update(Foreign foreign) {
        foreign.setType(MyConstants.CATEGORY_TYPE);
        foreignService.saveOrUpdate(foreign);
        return ResultJson.success();
    }

    @GetMapping("/select")
    @ResponseBody
    public ResultJson select() {
        return foreignService.getSelect(MyConstants.CATEGORY_TYPE);
    }
}
